<?php

declare (strict_types=1);

namespace app\controller\admin;

use think\facade\View;
use think\facade\Request;
use think\exception\ValidateException;
use app\validate\Upload as ValidateUpload;

class Upload extends Common
{
    public function uploadpic($name = 'image', $width = '100', $height = '100', $url = '')
    {
        $url = base64_decode($url);
        $msg = '';
        if (Request::isPost()) {
            $field = input('post.field');
            $files = request()->file();
            foreach ($files as $file) {
                $validate = new ValidateUpload();
                if (!$validate->check(['image' => $file])) {
                    $msg = $validate->getError();
                    break;
                }
                try {
                    $url = \think\facade\Filesystem::disk('public')->putFile('', $file);
                    $url = str_replace('\\', '/', '/static/upload/'.$url);
                    $name = $field;
                } catch (think\exception\ValidateException $e) {
                    $url = false;
                }
                break;
            }
        }

        View::assign('name', $name);
        View::assign('width', $width);
        View::assign('height', $height);
        View::assign('url', $url);
        View::assign('msg', $msg);
        return View::fetch();
    }

    public function uploadpics($name = 'images', $url = '')
    {
        if (Request::isPost()) {
            $files = request()->file();
            $url = [];
            foreach ($files as $file) {
                $validate = new ValidateUpload();
                if (!$validate->check(['image' => $file])) {
                    return json(['code' => 0,'url' => [],'msg' =>  $validate->getError()]);
                }
                try {
                    $savename = \think\facade\Filesystem::disk('public')->putFile('', $file);
                    $url[] = str_replace("\\", "/", '/static/upload/'.$savename);
                } catch (think\exception\ValidateException $e) {
                    //echo $e->getMessage();
                }
            }
            if (!empty($url)) {
                return json(['code' => 1,'url' => $url,'urls' => implode('|', $url),'msg' => '']);
            } else {
                return json(['code' => 0,'url' => [],'msg' => lang('上传失败')]);
            }
        }
        $url = base64_decode($url);
        View::assign('url', $url);
        View::assign('images', $url <> '' ? explode('|', $url) : []);
        View::assign('name', $name);
        return View::fetch();
    }

    public function file()
    {
        if (Request::isPost()) {
            $file = request()->file('file');
            try {
                $validate = new ValidateUpload();
                if (!$validate->check(['file' => $file])) {
                    return json(['location' => $validate->getError()]);
                }
                $url = \think\facade\Filesystem::disk('public')->putFile('', $file);
                $url = str_replace('\\', '/', '/static/upload/'.$url);
            } catch (ValidateException $e) {
                //echo $e->getMessage();
                $url = false;
            }
            return json(['location' => $url]);
        }
    }
}
